/**
 * UninaDocentiSync - A Java application to synchronize Unina resources
 * Copyright (C) 2011  Fabio Scippacercola (nonplay.programmer@gmail.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

package uninadocentisync;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.logging.Handler;
import java.util.logging.Level;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

/**
 *
 * @author Fabio Scippacercola (nonplay.programmer@gmail.com)
 */
public class ApplicationUtils {

    private ApplicationUtils()
    {
        FileInputStream fin = null;
        BufferedInputStream bin = null;
        ObjectInputStream oin = null;
        props = new HashMap<String, Object>();

        try
        {
            if ((new java.io.File(confFileName)).exists())
            {
                try
                {
                    fin = new FileInputStream(confFileName);
                    bin = new BufferedInputStream(fin);
                    oin = new ObjectInputStream(bin);

                    props = (HashMap<String, Object>)oin.readObject();
                }
                finally
                {
                    try
                    {
                        if (oin != null) oin.close();
                        if (bin != null) bin.close();
                        if (fin != null) fin.close();
                    }
                    catch (IOException e) {}
                }
            }
        }
        catch (Exception e)
        {
            this.log(Level.FINE, "Errore in lettura dati utente applicazione!", e);
        }
    }

    public static ApplicationUtils getInstance()
    {
        if (singleInstance == null)
            singleInstance = new ApplicationUtils();
        return singleInstance;
    }

    public synchronized void removeHandler(Handler hndlr) throws SecurityException {
        logger.removeHandler(hndlr);
    }

    public void savePropsOnFile()
    {
        FileOutputStream fout = null;
        BufferedOutputStream bout = null;
        ObjectOutputStream out = null;
        try
        {
            fout = new FileOutputStream(confFileName);
            bout = new BufferedOutputStream(fout);
            out = new ObjectOutputStream(bout);

            out.writeObject(props);
        }
        catch (Exception e)
        {
            this.log(Level.WARNING, "Errore in salvataggio applicazione!", e);
        }
        finally
        {
            try
            {
                if (out != null) out.close();
                if (bout != null) bout.close();
                if (fout != null) fout.close();
            }
            catch (IOException e) {}
        }
    }

    public void log(final Level level, final String string, final Throwable thrwbl) {
        logger.log(level, string, thrwbl);
    }

    public synchronized void addHandler(Handler hndlr) throws SecurityException {
        logger.addHandler(hndlr);
    }

    public Object removeProperty(String o) {
        return props.remove(o);
    }

    public Object putProperty(String k, Object v) {
        return props.put(k, v);
    }

    public Object getProperty(String o) {
        return props.get(o);
    }

    public boolean containsProperty(String o) {
        return props.containsKey(o);
    }

    public void clearProperties() {
        props.clear();
    }

    public Object lookAndStore(String properties, Object defaultValue)
    {
        Object o = getProperty(properties);
        if (defaultValue != null && (o == null || !o.getClass().equals(defaultValue.getClass())))
        {
            o = defaultValue;
            putProperty(properties, defaultValue);
        }
        return o;
    }

    public boolean isDebuggingEnabled() {
        return debuggingEnabled;
    }

    public void setDebuggingEnabled(boolean debuggingEnabled) {
        this.debuggingEnabled = debuggingEnabled;
        if (debuggingEnabled) logger.setLevel(Level.ALL);
    }
    
    private HashMap<String, Object> props;
    private boolean debuggingEnabled;

    private static final String confFileName = "uninaSync.dat";
    private static ApplicationUtils singleInstance = null;
    private static final java.util.logging.Logger logger = java.util.logging.Logger.getAnonymousLogger();
}
